import { IDefineOptions } from "sequelize-typescript/lib/interfaces/IDefineOptions";
import { Table as SequelizeTable, Column as SequelizeColumn, IPartialDefineAttributeColumnOptions, Model } from 'sequelize-typescript';
import _ from 'lodash';
import { DataTypeAbstract } from 'sequelize';

export function Table(options: IDefineOptions | any) { 
    const defaultOptions: IDefineOptions = {
        timestamps: true,
        underscored: true,
        underscoredAll: true,
        freezeTableName: false,
        paranoid: true,
    }

    //如果使用@Table形式
    if (options && options instanceof Model) { 
        let target = options;
        return SequelizeTable(defaultOptions)(target);
    }
    //形式为@Table(options)形式
    return SequelizeTable(Object.assign(defaultOptions, options));
}


export function Column(options: DataTypeAbstract | IPartialDefineAttributeColumnOptions | any, ...args: any[]) { 
    //@Column
    if (options && options instanceof Model) { 
        let target = options;
        return column({})(target, args[0]);
    }
    //@Column(options)
    return column(options);
}

function column(options: any) { 
    return function (target: any, propertyKey: string) { 
        const columName = _.snakeCase(propertyKey);
        options.field = columName;
        return SequelizeColumn(options)(target, propertyKey);
    }
}